package cn.jiedh.app.meiweidk.forgetpsw;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jiedh.app.meiweidk.MainActivity;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.Utils.tool.StringUtil;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.base.BaseActivity;
import cn.jiedh.app.meiweidk.login.LoginActivity;

/**
 * @name：重置密码
 * @author： 司马林
 * @phone： 18423134135
 * @createTime： 2017/10/19
 * @modifyTime： 2017/10/19
 */
public class ResetPswActivity extends BaseActivity {
    @BindView(R.id.bar_tv_title)
    TextView title;
    @BindView(R.id.new_psw)
    EditText newPswEdt;
    @BindView(R.id.coordinator)
    CoordinatorLayout coordinatorLayout;
    @BindView(R.id.reset_psw)
    Button resetPswBtn;

    private String forgetKey;

    @Override
    protected int getLayoutView() {
        return R.layout.activity_reset_psw;
    }

    @Override
    protected void initView(View view) {
        title.setText("重置密码");
        if (getIntent().getExtras() != null) {
            forgetKey = getIntent().getExtras().getString("forgetKey");
        }
        newPswEdt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    if (TextUtils.isEmpty(newPswEdt.getText().toString())) {
                        T.showShortCenterToast("请输入密码");
                    } else if (!StringUtil.isPassword(newPswEdt.getText().toString())) {
                        T.showShortCenterToast("请输入6-18位数字和字母组合的密码");
                    } else {
                        resetPsw(newPswEdt.getText().toString());
                    }
                    return true;
                }
                return false;
            }
        });
    }

    @OnClick({R.id.bar_img_back, R.id.reset_psw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_img_back:
                ActivityUtil.goBack(this);
                break;
            case R.id.reset_psw:

                InputMethodManager imm = (InputMethodManager) resetPswBtn.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(resetPswBtn.getWindowToken(), 0);

                String newPsw = newPswEdt.getText().toString();
                if (TextUtils.isEmpty(newPsw)) {
                    T.showShortCenterToast("请输入密码");
                } else if (!StringUtil.isPassword(newPsw)) {
                    T.showShortCenterToast("请输入6-18位数字和字母组合的密码");
                } else {
                    resetPsw(newPsw);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void resetPsw(String newPsw) {
        Map<String, Object> map = new HashMap<>();
        map.put("password", newPsw);
        map.put("forgetKey", forgetKey);
        request(this, UrlUtils.RESETPSW, map, true, response -> {
            T.showShort(response.getMessage());
            if (0 == response.getCode()) {
                SPUtils.clearUser(this);
                Intent intent = new Intent(ResetPswActivity.this, LoginActivity.class);
                Bundle bundle = new Bundle();
                bundle.putBoolean("isGoPswLogin", true);
                bundle.putBoolean("isGoHome", true);
                bundle.putInt("update2login",3);
                intent.putExtras(bundle);
                MobclickAgent.onEvent(activity,"my_setting_resetpwd_click");
                SPUtils.saveBuried("click","0","设置-新密码重置点击","","我的-设置",
                        "","","");
                startActivityForResult(intent, 1);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == LoginActivity.RESULT_CODE) {
            ActivityUtil.next(ResetPswActivity.this, MainActivity.class);
        }
    }
}
